﻿using System;
using System.Collections.Generic;
using System.Text;
using Npgsql;
using System.Windows.Forms;
using System.Data;

namespace Aplikacja_PostgreSQL
{
    public class Conn
    {
        public static void Connect(ref NpgsqlConnection conn, string serwer, string database, string user, string pass, string port)
        {
            try
            {
                string connstring = String.Format("Server={0};Port={1};" +
                        "User Id={2};Password={3};Database={4};",
                        serwer, port, user,
                        pass, database);
                conn = new NpgsqlConnection(connstring);
                conn.Open();
            }
            catch (Exception excp)
            {
                MessageBox.Show(excp.Message.ToString());
            }
        }
        public static bool Disconnect(ref NpgsqlConnection conn)
        {
            try
            {
                conn.Close();
                conn = null;
                return true;
            }
            catch (NullReferenceException excp)
            {
                return false;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
                return false;
            }
        }
        public static bool IsConnected(ref NpgsqlConnection conn)
        {
            try
            {
                if (conn.State == ConnectionState.Open)
                {
                    return true;
                }
                else
                    return false;

            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
